package com.ruoyi.web.core.config;

import com.adf.ADFServlet;
import com.adp.util.SetCharacterEncodingFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletContextInitializer;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;

@Configuration
public class WabacusConfig {


    @Bean
    public ServletListenerRegistrationBean servletListenerRegistrationBean() {
        ADFServlet adflistener = new  ADFServlet();
        return new ServletListenerRegistrationBean(adflistener);
    }

    @Bean
    public ServletRegistrationBean servletRegistrationBean() {
        ADFServlet adfServlet = new  ADFServlet();
        return new ServletRegistrationBean(adfServlet, "/AdpGo");
    }

    @Bean
    public ServletContextInitializer initializer() {
        return new ServletContextInitializer() {
            @Override
            public void onStartup(ServletContext servletContext) throws ServletException {
                //servletContext.setInitParameter("org.apache.activemq.brokerURL", brokerUrl);
                servletContext.setInitParameter("codgen.config", "com/adp/codgen/config/codgen-config.xml");
                servletContext.setInitParameter("configpath", "classpath{/adpconfig}");
                //servletContext.setAttribute(WebClient.CONNECTION_FACTORY_ATTRIBUTE, ((JmsPoolConnectionFactory) applicationContext.getBean(JmsPoolConnectionFactory.class)).getConnectionFactory());
            }
        };
    }
    @Bean
    public FilterRegistrationBean<SetCharacterEncodingFilter> encodingFilter(){
        FilterRegistrationBean<SetCharacterEncodingFilter> bean = new FilterRegistrationBean<SetCharacterEncodingFilter>();
        bean.setFilter(new SetCharacterEncodingFilter());
        bean.setName("Encoding");//过滤器名称
        bean.addUrlPatterns("/AdpGo");//过滤所有路径
        bean.setOrder(1);//优先级，最顶级
        return bean;
    }

}
